package minijava.util;

import java.util.HashMap;
import java.util.Map;

// how to beginScope, endScope?
public class Environment {
	private Map<String, Object> bindings = new HashMap();
	private Environment parentEnv;
	
	Environment(){
		parentEnv = null;
	}
	
	Environment(Environment parent){
		parentEnv = parent;
	}
	
	public void bind(String key, Object value){
		//TODO: some checks?
		bindings.put(key, value);
	}
	
	public Object lookup(String key){
		if (bindings.containsKey(key)){
			return bindings.get(key);
		} else {
			if (this.parentEnv != null){
				return this.parentEnv.lookup(key);
			} else {
				throw new RuntimeError( "Undefined variable '" + key + "'."); 
			}
		}
	}
}
